package cn.gdcp.lkqapp04;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Entity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final  int GET_RESULT = 1001;
    private EditText editText;
    private TextView textResult;
    private Handler handler;
    private Button btn_Get;
    private Button btn_Post;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_Get = findViewById(R.id.btn_get);
        btn_Get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phone = editText.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpGet(phone);
                    }
                }).start();
            }
        });


        btn_Post = findViewById(R.id.btn_post);
        btn_Post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phone = editText.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpPost(phone);
                    }
                }).start();
            }
        });

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_RESULT){
                    String html = (String) msg.obj;
                    textResult.setText(html);
                    return true;
                }
                return true;
            }
        });

        textResult = findViewById(R.id.txt_result);
        editText = findViewById(R.id.edt_number);

    }


    private void useHttpGet(String phone) {
        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=";
        url = url.concat(phone).concat("&userID=");
        /*String url = "http://www.baidu.com";*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int code = httpResponse.getStatusLine().getStatusCode();
            Log.e(TAG,"code = " + code);
            /*String htmlContent = getHtmlContent(httpResponse);
            Log.e(TAG, htmlContent);*/

            String htmlContent = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
            Log.e(TAG, htmlContent);


            Message message = Message.obtain();
            message.what = GET_RESULT;
            message.obj = htmlContent;
            handler.sendMessage(message);
            
        } catch (IOException e) {
            e.printStackTrace();
        }




    }

    private String getHtmlContent(HttpResponse httpResponse) throws IOException {
        InputStream inputStream = httpResponse.getEntity().getContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String line = bufferedReader.readLine();
        while (line != null){
            stringBuffer.append(line);
            line = bufferedReader.readLine();
        }
        inputStream.close();
        bufferedReader.close();
        return stringBuffer.toString();

    }


    private void useHttpPost(String phone) {
        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        HttpPost httpPost = new HttpPost(url);
        HttpClient httpClient = new DefaultHttpClient();
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("mobileCode",phone));
        nameValuePairList.add(new BasicNameValuePair("userID",""));

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
            HttpResponse httpResponse =httpClient.execute(httpPost);

            String htmlContent = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
            Log.e(TAG, htmlContent);

            Message message = Message.obtain();
            message.what = GET_RESULT;
            message.obj = htmlContent;
            handler.sendMessage(message);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
